package com.cockybook.poemspider.job;

import com.cockybook.poemspider.beans.Poet;
import com.cockybook.poemspider.dao.PoetsRepository;
import com.cockybook.poemspider.dao.UrlBeanRepository;
import com.cockybook.poemspider.model.IntroductionModel;
import com.cockybook.utils.Const;
import com.cockybook.utils.JsoupUtil;
import com.cockybook.utils.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import static com.cockybook.utils.StringUtil.getAuthIntName;

/**
 * 抓取作者信息
 */
//@Component
//@Order(1)
public class AuthorSpider implements CommandLineRunner {

    private final Logger logger= LoggerFactory.getLogger(getClass());

    @Autowired
    PoetsRepository poetsRepository;

    Map<String ,String> dynastys =new HashMap<String, String>();

    int MAXCOUNT=650;
//    int MAXCOUNT=2;

    public AuthorSpider(){
        this.dynastys.put("xianqinshiren","先秦");
        this.dynastys.put("hanchaoshiren","汉朝");
        this.dynastys.put("weijinshiren","魏晋");
        this.dynastys.put("nanbeichaoshiren","南北朝");
        this.dynastys.put("suichaoshiren","隋朝");
        this.dynastys.put("tangdaishiren","唐朝");
        this.dynastys.put("songdaishiren","宋朝");
        this.dynastys.put("jinchaoshiren","金朝");
        this.dynastys.put("liaochaoshiren","辽朝");
        this.dynastys.put("yuanchaoshiren","元朝");
        this.dynastys.put("mingchaoshiren","明朝");
        this.dynastys.put("qingchaoshiren","清朝");
        this.dynastys.put("xiandangdaishiren","近当代");
    }

    @Override
    public void run(String... args) throws Exception {
        for (int i = 1; i < MAXCOUNT; i++) {

            parseDynasty(String.format(Const.srcURLAuthorPREFINX,i),i, true);
        }


        //deal error
//        String s="866,1271,1359,1367,1480,1643,1650,1657,1701,2209,2480,2489,2490,2831,2835,3159,3215,3233,3245,3634,3636,4211,4226,4414,4430,4960,5067,5069,5121,5241,5349,5465,5469,5797,5798,5808,5965,6033,6364,6371,7873,8249,8256,8603,8907,9238,9604,9608,9610,9976,10412,10804,10806,11190,11192";
//        for (String ii :
//                s.split(",")) {
//            int i = Integer.parseInt(ii);
//            parseDynasty(String.format(Const.srcURLAuthorPREFINX,i),i, true);
//        }

    }

    /**
     * 解析朝代url
     * @param dynastyUrl
     * @param isGodownPage  是否下钻
     */
    public void parseDynasty(String dynastyUrl, int id ,boolean isGodownPage) throws Exception {
        Document document =null;
        try {
             document = JsoupUtil.urlGet(Const.srcURL+dynastyUrl);
        }catch (Exception e){
            logger.info("id:"+id,e);
            Poet poet = new Poet();
            poet.setId(id);
            poet.setMemo("Jsoup_get_error");
            addPoet(poet);
            return;
        }
        if(document==null){
            return ;
        }

        if(document.title().startsWith("暂未收录此作者")){
            Poet poet = new Poet();
            poet.setId(id);
            poet.setMemo("暂未收录此作者");
            addPoet(poet);
            return;

        }

        //document = Jsoup.parse(new File("/Users/corey/Documents/gits/poemspider/src/test/resources/bjy.html"),"utf8");
        if(document!=null){
            logger.info("Title: "+document.title());


            Poet poet = new Poet();
            poet.setId(id);
            //author name
            Element nameEle = document.getElementById("zzform_zz");
            String name= nameEle.val();  //
            poet.setName(name);

            //dynastyURL
            String dynastyURL=document.getElementById("daohang").getElementsByAttributeValueContaining("href","/category/").attr("href");
            poet.setDynasty(StringUtil.getDynasty(dynastyURL));
            poet.setDynastyReal(this.dynastys.get(poet.getDynasty()));

            //poems total
            String  num=document.getElementById("zzform").getElementsByTag("b").text();
            if(StringUtils.isNumeric(num)){
                poet.setTotal(Integer.parseInt(num));
            }

            //introduction
            Elements zuozhe = document.getElementsByClass("zuozhe");
            if(zuozhe.size()!=0){
                poet.setIntroduction(zuozhe.text());
                IntroductionModel.filterAll(poet.getIntroduction(),poet);

                Elements img = zuozhe.get(0).getElementsByTag("img");
                if(img!=null){
                    poet.setHeaderImgUrl(img.attr("src"));
                }
            }

            addPoet(poet);
        }
    }


    /**
     * 添加一个诗人
     * @param poet
     * @throws Exception
     */
    public void addPoet(Poet poet) throws Exception {

        if (! poetsRepository.exists(poet.getId())){
            poetsRepository.saveAndFlush(poet);
        }

        logger.info(poet.toString());

    }

    /**
     * 删除然后插入
     * @param poet
     * @param isDelete
     * @throws Exception
     */
    public void addPoet(Poet poet,boolean isDelete) throws Exception {


        if (poetsRepository.exists(poet.getId())){
            poetsRepository.delete(poet.getId());
        }

        poetsRepository.saveAndFlush(poet);

        logger.info(poet.toString());

    }






}
